#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <campaign.h>

#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    m_model = new DTwentyModel(this);
    m_model->setRoot(new Campaign(m_model));

    updateActions();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::updateActions()
{
    foreach (QAction* action, ui->mainToolBar->actions())
    {
        action->deleteLater();
    }
    ui->mainToolBar->clear();


    QVariant v = m_model->data(QModelIndex(), DTwentyModel::Role_CHILD_OPTIONS);
    if (!v.canConvert(QVariant::Map))
    {
        qDebug() << Q_FUNC_INFO << "FAIL!";
        return;
    }

    QVariantMap m = v.toMap();
    if (m.isEmpty())
    {
        qDebug() << Q_FUNC_INFO << "empty map!";
        return;
    }

    foreach (QString name, m.keys())
    {
        QAction* action = ui->mainToolBar->addAction(name);
        action->setData(m.value(name));
    }


}
